﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
    public class CellShelveManager : GDGeek.Singleton<CellShelveManager>
    {
        [SerializeField]
        private BoxManager _manager;
        private Cell cell_ = null;

        public void selected(BoxLocation oldValue, BoxLocation newValue)
        {

            var oldCell = _manager.getCell(oldValue);
            var newCell = _manager.getCell(newValue);
            if (oldCell != null)
            {
                down(oldCell);
            }

            if (newCell != null)
            {
                up(newCell, "");
            }
         
        }
        public void up(Cell cell, string text) {
            cell_ = cell;
            CellDraw draw = cell.gameObject.GetComponent<CellDraw>();
           // draw.close();
            draw.show(true, cell.location.location.type, delegate(BoxLocation localtion) {


              
            });
            draw.setShelve(text, cell.location.location);

        }

        private BoxLocation _localtion;
        public void doClicked(BoxLocation localtion) {

            //Debug.LogError("ABA");
            if (ShelveModel.IsInitialized)
            {
                if (_localtion.box == -1)
                {
                    _localtion = localtion;
                    ShelveModel.Instance.doSelected(_localtion);
                  
                }
                else if(_localtion == localtion)
                {
                    _localtion.box = -1;
                    ShelveModel.Instance.doSelected(_localtion);
                  
                }
              
               
            }

        }
        public void open() {
            _localtion.box = -1;
            Cell[] cells = _manager.gameObject.GetComponentsInChildren<Cell>();
            foreach (var cell in cells) {
                CellDraw cd = cell.GetComponent<CellDraw>();
                cd.close();
              
                cd.clicked.gameObject.SetActive(true);
                cd.callback += doClicked;
            }

            Box[] boxs = _manager.gameObject.GetComponentsInChildren<Box>();

            foreach (var box in boxs)
            {
                box.close();
            }
        }
        public void close() {

            _localtion = new BoxLocation();
            _localtion.box = -1;
            ShelveModel.Instance.doSelected(_localtion);


            if (cell_ != null) {
                down(cell_);
            }

            Cell[] cells = _manager.gameObject.GetComponentsInChildren<Cell>();
            foreach (var cell in cells)
            {  
                CellDraw cd = cell.GetComponent<CellDraw>();
                cd.close();
                cd.clicked.gameObject.SetActive(false);
                cd.callback -= doClicked;
            }

            Box[] boxs = _manager.gameObject.GetComponentsInChildren<Box>();

            foreach (var box in boxs)
            {

                box.open();
            }
        }
        public void down(Cell cell){

            CellDraw draw = cell.gameObject.GetComponent<CellDraw>();
            draw.close();

        }
        public void onScanList(string[] list)
        {


            if (cell_ != null && list != null && list.Length != 0)
            {
                up(cell_, list[list.Length - 1]);
               // cell_.shelve.setText(list[list.Length - 1]);
            }
        }
    }
}